package com.hc.dao;

import com.hc.bean.ShopCart;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface ShopCartDao {
    /*添加购物车*/
    @Insert("insert into shopCart values(null,#{productId},#{userId},#{productNum},#{isCollect},#{isShopCart})")
    @Options(useGeneratedKeys = true,keyProperty = "shopCartId",keyColumn = "shopCartId")
    Integer addShopCart(ShopCart shopCart);

    /*修改购物车*/
    @Update("<script>" +
            " update shopCart " +
            " <set>" +
            " <if test='productId!= null'>" +
            " productId=#{productId}," +
            " </if>" +
            " <if test='userId!= null'>" +
            " userId=#{userId}," +
            " </if>" +
            " <if test='productNum!= null'>" +
            " productNum=#{productNum}," +
            " </if>" +
            " <if test='isCollect!= null'>" +
            " isCollect=#{isCollect}," +
            " </if>" +
            " <if test='isShopCart!= null'>" +
            " isShopCart=#{isShopCart}," +
            " </if>" +
            " </set>" +
            " where shopCartId=#{shopCartId}" +
            " </script>")
    Integer updateShopCart(ShopCart shopCart);

    /*移出购物车*/
    /*需要使用$实现SQL注入需要加上@Param注解*/
    @Delete("delete from shopCart where shopCartId in (${shopCartIds})")
    Integer deleteShopCart(@Param("shopCartIds") String shopCartIds);

    /*根据商品id查询,判断该商品是否已在购物车*/
    @Select("select * from shopCart where userId=#{userId} and productId=#{pid}")
    ShopCart getShopCartByPid(@Param("userId") Integer userId,@Param("pid") Integer pid);

    /*根据用户id查询指定用户的购物车*/
    @Select("select * from shopCart where isShopCart=1 and userId=#{uid}")
    @Results({
            @Result(column = "productId",property = "productId"),
            @Result(column = "productId",property = "product",one = @One(select = "com.hc.dao.ProductDao.getProductById"))
    })
    List<ShopCart> getShopCartByUid(Integer uid);
}
